/*
Description: Specification with mass layoffs
Content: Table G.5
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


********************************************************************************
**# LOAD ANALYSIS DATASET
********************************************************************************

use "$final/finaldata_ieb_main_light.dta", replace


********************************************************************************
*** MERGE WITH FIRM CLOSURES AND FIRM BANKRUPTCIES DATA
********************************************************************************

preserve 

use "$data/BHP_firmclosures.dta", clear 

rename jahr jahr_close

tempfile bhp_closure
save `bhp_closure'

restore 

merge m:1 betnr using  `bhp_closure', gen(bhp_closure) keep(1 3)
merge m:1 betnr using"\\IAB\dfs\017\Ablagen\D01700-IAB-Projekte\D01700-Projekte-FDZ\Datensaetze\_Endprodukte\BHP\BHP7520_v1\Grundgesamtheit\bhp_7520_m06_iv_v1.dta", gen(bhp_bankrupt) keep(1 3)


bys persnr: egen closure_sample = max(bhp_closure)
bys persnr: egen bankrupt_sample = max(bhp_bankrupt)
	
	
********************************************************************************
*** IDENTIFY UNEMPLOYMENT SPELLS BASED ON WHETHER THEY ARE DUE TO PLANT CLOSURE OR NOT 
********************************************************************************
		
* First the employment spell which might be related to a closure
	
	bys betnr (startmy): egen jahr_max  = max(jahr_close)
		
	*bys betnr persnr (startmy): gen lastob_firm = 1 if _n == _N 
	
	cap drop end_year
	gen end_year = 1960 + floor(startmy/12)
	
	
	* Create a variable "displaced" that looks at 
	cap drop displaced
	gen displaced = 1 if (end_year == jahr_close | end_year == jahr_close -1) 
	replace displaced = 1 if (end_year == iv_meldung_endjahr | end_year == iv_meldung_endjahr) 
	replace displaced = 1 if (end_year == iv_geld_endjahr | end_year == iv_geld_endjahr -1) 
	 
	cap drop displaced_sample
	bys persnr (startmy): egen displaced_sample = max(displaced)

		
********************************************************************************
**# Table G.5, Panel A, Columns 1,2
********************************************************************************	

cap clonevar ln_firmsize_lb = ln_firmsize	

eststo clear
local i=1
foreach out in  unemp_1m unemp_3m  {
foreach control in control control_ext {

global control = "i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize"
global control_ext = "i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb"

qui eststo mod`i': reghdfe `out' treatment_terror_m3y_p90 ${`control'} if  k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & displaced_sample ==  1, absorb(i.ieb_nat_id#i.year2 i.year2#i.month i.state)  vce(cluster clusterterror_m3y_p90)

sum	 `out' if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 & treatment_terror_m3y_p90 == 0 & displaced_sample == 1

estadd scalar ymean = r(mean)
estadd local `control' "Yes"
estadd local yearorigin "Yes"
estadd local yearmonth "Yes"
estadd local state "Yes"

local i=`i'+1

	}
	}
	
	
**# TABLE
esttab mod* using "$table/iebprepost_alttreat_final.tex", star(* 0.10 ** 0.05 *** 0.01) se replace b(3) drop(*edu_max age frau *ysm_atunemp ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb _cons) nocons  nogaps nonotes noob nomtitle ///
mgroups("\shortstack{Unemp. due to mass layoff\\in month with terror event}", pattern(1 0)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
mtitles("\shortstack{Emp. within\\1m}" "\shortstack{Emp. within\\3m}") ///
varlabel(treatment_terror_m3y_p90 "Post-Terror") scalars("nobs Observations" "ymean Mean outcome") ///
sfmt(%9.0fc %9.3f)  prehead(" \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \hline\hline")

